const commonFn = {
  j2s(obj) {
    return JSON.stringify(obj)
  },
  shallowRefresh(name) {
    router.replace({ path: '/refresh', query: { name: name }})
  },
  refresh(name, params) {
    router.replace({ path: '/refresh', query: { name: name, params: params }})
  },
  refreshFullPath(fullPath) {
    router.replace({ path: '/refresh', query: { fullPath: fullPath }})
  },
  closeGlobalLoading() {
    setTimeout(() => {
      store.dispatch('showLoading', false)
    }, 0)
  },
  openGlobalLoading() {
    setTimeout(() => {
      store.dispatch('showLoading', true)
    }, 0)
  },
  cloneJson(obj) {
    return JSON.parse(JSON.stringify(obj))
  },
  toastMsg(type, msg) {
    switch (type) {
      case 'normal':
        bus.$message(msg)
        break
      case 'success':
        bus.$message({
          message: msg,
          type: 'success'
        })
        break
      case 'warning':
        bus.$message({
          message: msg,
          type: 'warning'
        })
        break
      case 'error':
        bus.$message.error(msg)
        break
    }
  },
  removeByValue(arr, val, type, key) {
    for (let i = 0; i < arr.length; i++) {
      if (type == 1) {
        if (arr[i] == val) {
          arr.splice(i, 1)
          break
        }
      } else {
        if (arr[i][key] == val) {
          arr.splice(i, 1)
          break
        }
      }
    }
  },
  clearVuex(cate) {
    store.dispatch(cate, [])
  },
  getHasRule(val) {
    const moduleRule = 'admin'
    let userInfo = Lockr.get('userInfo')
    if (userInfo.id == 1) {
      return true
    } else {
      let authList = moduleRule + Lockr.get('authList')
      return _.includes(authList, val)
    }
  },
  getIndexByKey(arr, key, value) {
    for (let index = 0; index < arr.length; index++) {
      if (arr[index][key] == value) {
        return index
      }
    }
    return -1
  }
}

export default commonFn
